﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace theguineasyndrome
{
    public class MenuMode : AppMode
    {
        private Song song;

        private Texture2D tgsTexture;
        private Vector2 tgsOrigin;

        private Texture2D logoTexture;
        private Vector2 logoOrigin;

        private Texture2D sunTexture;
        private Animation sunAnimation;

        private Texture2D pushspaceTexture;
        private Vector2 pushspaceOrigin;

        private Texture2D backgroundTexture;

        public MenuMode(TheGuineaSyndromeGame game, GraphicsDevice graphicsDevice, ContentManager content, GameWindow Window)
            : base(game, graphicsDevice, content, Window)
        {
            screenRectangle.Width = graphicsDevice.Viewport.Width;
            screenRectangle.Height = graphicsDevice.Viewport.Height;
        }

        public override bool isFullScreen()
        {
            return true;
        }

        public override void Initialize()
        {
            base.Initialize();

            MediaPlayer.Stop();
            MediaPlayer.Play(song);

            sunAnimation = new Animation(sunTexture, new Vector2(100, 100), 80, true);
            sunAnimation.setOrigin(new Vector2(50, 50));
            sunAnimation.setPosition(new Vector2(-100, -100));
        }

        public override void LoadContent()
        {
            base.LoadContent();

            tgsTexture = content.Load<Texture2D>("images/tgs");
            tgsOrigin = new Vector2(tgsTexture.Width, tgsTexture.Height) / 2;

            logoTexture = content.Load<Texture2D>("images/logo");
            logoOrigin = new Vector2(logoTexture.Width, logoTexture.Height) / 2;

            sunTexture = content.Load<Texture2D>("images/sun");

            pushspaceTexture = content.Load<Texture2D>("images/pushspace");
            pushspaceOrigin = new Vector2(pushspaceTexture.Width, pushspaceTexture.Height) / 2;

            backgroundTexture = content.Load<Texture2D>("images/background");

            song = content.Load<Song>("music/Peanuts groove");
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (isJustPressed(Keys.Escape))
                game.Exit();

            if (isJustPressed(Keys.Space) || isJustPressed("LeftButton"))
                game.setMode(new GameMode(game, graphicsDevice, content, window));

            sunAnimation.Update(gameTime);

            double t = gameTime.TotalGameTime.TotalSeconds / 2 - Math.PI * 2 / 3;
            sunAnimation.setPosition(new Vector2((float)(graphicsDevice.Viewport.Width / 2 + Math.Cos(t) * 250), (float)(graphicsDevice.Viewport.Height / 2 + Math.Sin(t) * 250)));
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullNone);
            spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
            spriteBatch.End();

            spriteBatch.Begin();

            spriteBatch.Draw(tgsTexture, new Vector2(graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Height / 2 + 180), null, Color.White, 0, tgsOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(logoTexture, new Vector2(graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Height / 2 - 40), null, Color.White, 0, logoOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(pushspaceTexture, new Vector2(graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Height / 2 + 20), null, Color.White, 0, pushspaceOrigin, 1, SpriteEffects.None, 0);

            sunAnimation.Draw(spriteBatch, camera);

            spriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
